<?php
/*
Copyright 2008, Nathalia


Author:
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)

*/
class Listar extends TPage
{
	public function onInit($param)
    {
        parent::onInit($param);
        if(!$this->IsPostBack)  // if the page is requested the first time
        {
            // get the total number of posts available
            $this->Repeater->VirtualItemCount = AnaisRecord::finder()->count();
            // populates post data into the repeater
            $this->populateData();
        }
    }
 
    public function pageChanged($sender,$param)
    {
        // change the current page index to the new one
        $this->Repeater->CurrentPageIndex=$param->NewPageIndex;
        // re-populate data into the repeater
        $this->populateData();
    }
 
    protected function populateData()
    {
        $offset = $this->Repeater->CurrentPageIndex * $this->Repeater->PageSize;
        $limit  = $this->Repeater->PageSize;
        if($offset+$limit > $this->Repeater->VirtualItemCount)
            $limit = $this->Repeater->VirtualItemCount - $offset;
        $this->Repeater->DataSource = $this->getAnais($offset,$limit);
        $this->Repeater->dataBind();
    }

    protected function getAnais($offset, $limit)
    {
        // Construts a query criteria
        $criteria=new TActiveRecordCriteria;
        $criteria->OrdersBy['AnaisID']='asc';
        $criteria->Limit=$limit;
        $criteria->Offset=$offset;
        if(!$this->Request['all'] == 1)
        {
            $criteria->Condition = 'Status = 1';
        }
        // query for the posts with the above criteria and with author information
        return AnaisRecord::finder()->findAll($criteria);
    }

}
?>
